﻿Imports System.Xml.Serialization
Imports System.IO
Imports System.Xml

Public Class SerializeHelper

    Public Shared Function ObjToXML(obj As Object) As String
        Using ms As New MemoryStream
            Dim en As New XmlSerializerNamespaces
            en.Add("", "")
            With New XmlSerializer(obj.GetType)
                .Serialize(ms, obj, en)
            End With
            Return System.Text.Encoding.UTF8.GetString(ms.ToArray)
        End Using
    End Function

    Public Shared Function ObjToXMLElement(obj As Object) As XmlElement
        With New XmlDocument
            .LoadXml(ObjToXML(obj))
            Return .DocumentElement
        End With
    End Function

End Class
